using System;

namespace TrueCode.Core.EditableEntity
{
  [Serializable]
  public class EditableValue< T > : IEditableValue
  {
    private T _current;
    private T _original;

    public EditableValue(){}
    public EditableValue( T value ) : this()
    {
      _original = value;
      _current  = value;
    }

    public static implicit operator T( EditableValue< T > editable )
    {
      return editable._current;
    }

    public T Value
    {
      get { return _current; }
      set
      {
        if( ! Equals( _current, value ) )
        {
          _current = value;
          IsDirtyChanged.Raise( this );
        }
      }
    }

    public bool IsDirty
    {
      get{ return !Equals( _original, _current ); }
    }

    public event EventHandler IsDirtyChanged;

    public void AcceptChanges()
    {
      _original = _current;
    }

    public void RejectChanges()
    {
      _current = _original;
    }
  }
}